package com.zm.demo.core.leetcode;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/15 10:03
 */
public class MyAtoi {

    public int myAtoi(String str) {

        if(str == null || str.trim().length() == 0){
            return 0;
        }

        str = str.trim();
        if(effectiveChar(str.charAt(0))){
            if(str.charAt(0) == '-'){
                StringBuilder sb = new StringBuilder();
                for(int i=1; i<str.length(); i++){
                    if(isNumber(str.charAt(i))){
                        if(sb.length() != 0 || str.charAt(i) != '0'){
                            sb.append(str.charAt(i));
                        }
                    }else{
                        break;
                    }
                }
                if(sb.length() == 0){
                    return 0;
                }else if (outOfRange("-" + sb.toString(), String.valueOf(Integer.MIN_VALUE))){
                    return Integer.MIN_VALUE;
                }else {
                    return -Long.valueOf(sb.toString()).intValue();
                }



            }else if (str.charAt(0) == '+'){
                StringBuilder sb = new StringBuilder();
                for(int i=1; i<str.length(); i++){
                    if(isNumber(str.charAt(i))){
                        if(sb.length() != 0 || str.charAt(i) != '0'){
                            sb.append(str.charAt(i));
                        }
                    }else{
                        break;
                    }
                }
                if(sb.length() == 0){
                    return 0;
                }else if (outOfRange(sb.toString(), String.valueOf(Integer.MAX_VALUE))){
                    return Integer.MAX_VALUE;
                }else {
                    return Integer.valueOf(sb.toString());
                }
            }else{
                StringBuilder sb = new StringBuilder();
                for(int i=0; i<str.length(); i++){
                    if(isNumber(str.charAt(i))){
                        if(sb.length() != 0 || str.charAt(i) != '0'){
                            sb.append(str.charAt(i));
                        }
                    }else{
                        break;
                    }
                }
                if(sb.length() == 0){
                    return 0;
                }else if (outOfRange(sb.toString(), String.valueOf(Integer.MAX_VALUE))){
                    return Integer.MAX_VALUE;
                }else {
                    return Integer.valueOf(sb.toString());
                }
            }
        }else{
            return 0;
        }


    }

    private boolean effectiveChar(char c){
        return  c == '-' || c == '+' || (c>='0' && c<='9');
    }

    private boolean isNumber(char c){
        return  (c>='0' && c<='9');
    }

    private boolean outOfRange(String s, String i){

        if (s.length() > i.length()) {
            return true;
        }else if (s.length() < i.length()) {
            return false;
        }else {
            for (int j = 0; j < s.length(); j++) {
                if (s.charAt(j) < i.charAt(j)){
                    return false;
                }else if (s.charAt(j) > i.charAt(j)){
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[]args){
        out.println(new MyAtoi().myAtoi("1095502006p8"));
    }
}
